package com.heima.article.listener;

import com.heima.article.service.ApArticleService;
import com.heima.common.constants.mq.PublishArticleConstants;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Slf4j
@Component
public class PublishQueueListener {
    @Resource
    ApArticleService apArticleService;

    @RabbitListener(queuesToDeclare = @Queue(name = PublishArticleConstants.PUBLISH_ARTICLE_QUEUE))
    public void listener(@Payload String newsId, Message message, Channel channel){
        log.info("接收到发布文章通知， 待发布文章id: {} , 当前时间: {}",newsId, LocalDateTime.now().toString());
        try {
            apArticleService.publishArticle(Integer.valueOf(newsId));
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
